package com.pe.pgn.clubpgn.dao.impl;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.dao.TipoVehiculoDao;
import com.pe.pgn.clubpgn.dao.hibernate.GenericDaoHibernate;
import com.pe.pgn.clubpgn.domain.ClpbTipoVehiculo;

@Repository
public class TipoVehiculoDaoImpl extends GenericDaoHibernate<ClpbTipoVehiculo, Long> implements
		TipoVehiculoDao {

	public TipoVehiculoDaoImpl(){
		super(ClpbTipoVehiculo.class);
	}
	
	public void guardarTipoVehiculo(ClpbTipoVehiculo tipoVehiculo){
		
		if(tipoVehiculo.getId() == null){
			tipoVehiculo.setCoUsuarioCreador(getUsuarioLogueado().getUsername());
			tipoVehiculo.setDaFechaCreacion(Calendar.getInstance());
		}
		else{
			tipoVehiculo.setCoUsuarioModificador(getUsuarioLogueado().getUsername());
			tipoVehiculo.setDaFechaModificacion(Calendar.getInstance());
		}	
		
		save(tipoVehiculo);
		getHibernateTemplate().flush();
	}
	
	public ClpbTipoVehiculo obtenerTipoVehiculo(Long id){
		
		ClpbTipoVehiculo tipoVehiculo = get(id);
		return tipoVehiculo;
	}
	
	public void eliminarTipoVehiculo(Long id){
		
		remove(id);
		getHibernateTemplate().flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<ClpbTipoVehiculo> obtenerTiposVehiculo(){
		
		Criteria criteria = DetachedCriteria.forClass(ClpbTipoVehiculo.class)
		.getExecutableCriteria(getSessionFactory().getCurrentSession());

		criteria.setMaxResults(CLPConstantes.MAXIMO_RESULTADO_INICIAL);
		criteria.addOrder(Order.desc("id"));
		
		List<ClpbTipoVehiculo> lista = criteria.list(); 
		return lista;
	}

	@SuppressWarnings("unchecked")
	public List<ClpbTipoVehiculo> obtenerListaTiposVehiculo() {

		StringBuffer query = new StringBuffer();
		query.append(" select ");
		query.append(" 	id, de_tipo_vehiculo as deTipoVehiculo, ");
		query.append("	st_es_default as strDefault, st_tipo_vehiculo as strTipoVehiculo ");
		query.append(" from clpb_tipo_vehiculo ");
		query.append(" where st_tipo_vehiculo = '").append(CLPConstantes.ST_VERDADERO).append("'");
		
		return findListOfBeans(query.toString(), null, ClpbTipoVehiculo.class);
	}
	
	
}
